﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂随机形状面积
{
    public class Shape
    {
        private double a = 0;
        private double b = 0;
        private double c = 0;

        public double A { get { return a; } set { a = value; } }
        public double B { get { return b; } set { b = value; } }
        public double C { get { return c; } set { c = value; } }

        public virtual double Area()
        {
            double area = 0;
            return area;
        }
    }

    class Rectangle : Shape
    {
        public override double Area()
        {
            Console.WriteLine("长方形的长为：{0}   宽为：{1}", A, B);
            double area = 0;
            area = A * B;
            return area;
        }
    }

    class Squre : Shape
    {
        public override double Area()
        {
            Console.WriteLine("正方形的边长为：" + A);
            double area = 0;
            area = A * A;
            return area;
        }
    }

    class Triangle : Shape
    {
        public override double Area()
        {
            Console.WriteLine("三角形的三条边长为：{0} {1} {2}", A, B, C);
            double p = (A + B + C) / 2;
            double area = 0;
            area = Math.Sqrt(p * (p - A) * (p - B) * (p - C));
            return area;
        }
    }

    class Circle : Shape
    {
        public override double Area()
        {
            Console.WriteLine("圆的半径为:" + A);
            double area = 0;
            area = 3.14 * A * A;
            return area;
        }
    }

    public class ShapeFactory   //形状工厂，主要是选择性的新建某个指定的类
    {
        public static Shape CreateShape(string shapeType)
        {
            Shape shape = null;
            switch (shapeType)
            {
                case "Rectangle":
                    {
                        shape = new Rectangle();
                        break;
                    }
                case "Squre":
                    {
                        shape = new Squre();
                        break;
                    }
                case "Triangle":
                    {
                        shape = new Triangle();
                        break;
                    }
                case "Circle":
                    {
                        shape = new Circle();
                        break;
                    }
                default:
                    {
                        Console.WriteLine("错误");
                        break;
                    }
            }
            return shape;
        }

    }

    internal class Program
    {

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("随机生成十个图形\n");
                double areaSum = 0;
                for (int i = 0; i < 10; i++)
                {
                    string[] _shape = { "Rectangle", "Squre", "Triangle", "Circle" };
                    Random rand = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")) + i);
                    double _A = rand.Next(1, 1000);
                    double _B = rand.Next(1, 1000);
                    double _C = rand.Next(1, 1000);
                    double _AREA = 0;

                    int x = rand.Next(3);

                    // 三角形三边关系
                    while ((x == 2) && !((_A + _B > _C) && (_A + _C > _B) && (_B + _C > _A)))
                    {
                        _A = rand.Next(1, 100);
                        _B = rand.Next(1, 100);
                        _C = rand.Next(1, 100);
                    }
                    Shape shape;
                    shape = ShapeFactory.CreateShape(_shape[x]);
                    shape.A = _A;
                    shape.B = _B;
                    shape.C = _C;
                    Console.WriteLine("生成的第{0}个图形为:{1}", i + 1, _shape[x]);
                    _AREA = shape.Area();
                    Console.WriteLine("其面积为：" + _AREA + "\n");
                    areaSum += _AREA;
                }
                Console.WriteLine("十个图形的面积和为：" + areaSum);
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误！" + ex.Message);
            }
            Console.ReadLine();
        }
    }
}
